package com.shuqing.controller;

import com.shuqing.domain.User;
import com.shuqing.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public String login(User user, HttpServletRequest request){
        User loginUser = userService.login(user);
        if(loginUser != null){
            request.getSession().setAttribute("USER_SESSION", loginUser);
            return "redirect:/admin/main.jsp";
        }
        request.setAttribute("msg", "用户名或密码错误");
        return "redirect:/admin/login.jsp";
    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
//        获取 session (包含用户信息缓存)
        HttpSession session = request.getSession();
//        删除用户信息
        session.removeAttribute("USER_SESSION");
//        销毁 session
        session.invalidate();
        return "forward:/admin/login.jsp";
    }

}
